package com.qf.xiaofeizhe.consumer;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.xiaofeizhe.dbvote.entity.Vote;
import com.qf.xiaofeizhe.dbvote.service.IVoteService;
import com.rabbitmq.client.Channel;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@Component
public class VoteLogConsumer {

    @Resource
    IVoteService voteService;

    @RabbitListener(queues = "vote")
    public void getMsg(Channel channel, Message message, String votejson) throws Exception {

        //json -->Vote对象
        ObjectMapper objectMapper = new ObjectMapper();

        List<Vote> jsonObjects = objectMapper.readValue(votejson, new TypeReference<List<Vote>>(){});



        //落库
        voteService.saveBatch(jsonObjects);

        //手动ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }

}
